*5_Wellman___APSR___Maps
*July 21, 2020

***********************************
*This File creates the Maps in Figure A1 and Figure A2.3
***********************************

// ssc install spmap
// ssc install shp2dta

* Set the working directory to the main folder with the data files

***************
* Data Prep
***************
	//download africancountires SHP files from https://gauthiervaillant.carto.com/tables/africancountires/public
	shp2dta using africancountires, data(africadata) coor(africacoor) genid(id) replace
	*(old files)shp2dta using AfricanCountriesShapeFiles/AfricanCountires, data(africadata) coor(africacoor) genid(id)

	use africadata, clear

	replace country = "Cote d'Ivoire" if country=="Côte d'Ivoire"
	replace country = "Democratic Republic of Congo" if country=="Congo DRC"
	drop if country=="Canarias"
	drop if country=="Yemen"
	drop if country=="Réunion"
	drop if country=="Madeira"
	drop if country=="Seychelles"
	drop if country=="Spain"
	drop if inlist(land_rank, 1,2)
	drop if objectid==167
	drop if objectid==171
	drop if objectid==518
	run "z_stateid_crosswalk.do"

	spmap using africacoor, id(id)

	save, replace

	clear
	use Wellman_Maps_Base
	merge m:m stateid using africadata
	drop _m

	save Wellman_Maps_Working.dta, replace

***************
*Maps for Figure A1: Extension of de jure emigrant enfranchisement
***************

// Prep time-period data for graphing
	use Wellman_Maps_Working.dta, clear
	foreach year in 1990 1995 2000 2005 2010 2015 {
		cap drop evrr1`year'
		
		gen evrr1`year'=0

		list id if year==`year' & evrr1==1
		if `year' == 1990 local visit 41 91 141 17  
		if `year' == 1995 local visit 1 2 41 50 91 85 7 44 120 23 128 131 133 10 61 141 15 17  
		if `year' == 2000 local visit 1 2 41 48 50 91 5 85 7 69 44 120 23 128 131 133 61 141 15 17
		if `year' == 2005 local visit 1 2 41 48 50 91 5 84 85 86 93 7 69 44 120 23 128 131 133 59 61 141 15 17
		if `year' == 2010 local visit 1 2 41 49 48 50 91 5 84 85 86 93 7 69 21 44 120 106 24 23 128 131 133 59 10 61 141 81 15 17 
		if `year' == 2015 local visit 1 2 41 49 48 50 55 91 5 84 85 86 93 7 6 9 63 69 72 21 44 120 106 112 24 23 46 128 131 133 59 10 61 29 34 141 81 15 17 
		
		foreach y of local visit{
			replace evrr1`year' = 1 if id==`y'
		}

		*Arab Country* 
		if `year' != 2015 local visit 1 9 112 46 81 17 34
		if `year' == 2015 local visit 1 9 112 46 81 17 
		
		foreach y of local visit{
			replace evrr1`year'  = 2 if id==`y'
		}

	}

	save Wellman_Maps_Working.dta, replace
// Generate maps	
	foreach year in 1990 1995 2000 2005 2010 2015 {
		*****************
		*A1: `year' Map*
		*****************
		use Wellman_Maps_Working.dta, clear
		keep if year==`year'
		spmap evrr1`year' using africacoor.dta, id(id) fcolor(Greys) ///
			title("`year'", si(*2)) cln(12) legend(symy(*2) symx(*2) size(*1.5) ///
			label(4 "North Africa (N/A)"))legend(label(3 "De jure enfranchisement")) ///
			legend(label(2 "No emigrant enfranchisement"))
		graph export evrr1`year'.pdf, as(pdf) replace
	}

***************
*Maps for Figure A2.3: Emigrant Polling Stations (1990 - 2015)
***************
	use Wellman_Maps_Working.dta, clear
	merge m:m stateid year using Wellman_APSR_full_working.dta
	drop _m
	gen Polls_Map = Polls

	replace Polls_Map=0 if evrr1==0
	replace Polls_Map=0 if evrr2==0
	replace Polls_Map=2 if country=="Cape Verde" 
	replace Polls_Map=1 if country=="Sao Tome" 
	replace Polls_Map=1 if country=="Djibouti" & year>=1992
	replace Polls_Map=2 if country=="Guinea-Bissau" & year>=2014
	replace Polls_Map=0 if country=="Guinea-Bissau" & year==2013
	replace Polls_Map=2 if country=="Guinea-Bissau" & year>=1994 & year<=1998
	replace Polls_Map=2 if country=="Mali"
	replace Polls_Map=1 if country=="Mali" & year>=1991 & year<=1996
	replace Polls_Map=2 if country=="Senegal" & year>=1993 
	replace Polls_Map=1 if country=="Benin" & year>=1991 & year<=1995
	replace Polls_Map=1 if country=="Benin" & year>=2011 
	replace Polls_Map=2 if country=="Mauritania" & year>=2009 & year<=2012
	replace Polls_Map=1 if country=="Mauritania" & year>=2013
	replace Polls_Map=2 if country=="Ivory Coast" & year>=2010
	replace Polls_Map=1 if country=="Guinea" & year>=1993 & year<=2009
	replace Polls_Map=2 if country=="Guinea" & year>=2010
	replace Polls_Map=2 if country=="Botswana" & year>=1999 & year<=2008
	replace Polls_Map=1 if country=="Botswana" & year>=2009
	replace Polls_Map=1 if country=="Burundi" & year>=1993 & year<=2004
	replace Polls_Map=0 if country=="Burundi" & year>=2005 & year<=2009
	replace Polls_Map=1 if country=="Burundi" & year>=2009
	replace Polls_Map=2 if country=="Cameroon" & year>=2011
	replace Polls_Map=1 if country=="Central African Republic" & year>=2005 & year<=2014
	replace Polls_Map=2 if country=="Central African Republic" & year>=2015 
	replace Polls_Map=1 if country=="Chad" & year>=1996
	replace Polls_Map=1 if country=="Gabon" & year>=1998
	replace Polls_Map=1 if country=="Kenya" & year>=2013
	replace Polls_Map=2 if country=="Mozambique" & year>=2004
	replace Polls_Map=1 if country=="Namibia" & year>=2009
	replace Polls_Map=1 if country=="Rwanda" & year>=2003 & year<=2009
	replace Polls_Map=2 if country=="Rwanda" & year>=2010
	replace Polls_Map=0 if country=="South Africa" & year==1993
	replace Polls_Map=1 if country=="South Africa" & year>=2009
	replace Polls_Map=2 if country=="South Africa" & year>=1994 & year<=1996

	save Wellman_Maps_Working_Polls, replace 


// Data Prep
	use Wellman_Maps_Working_Polls.dta, clear
	foreach year in 1990 1995 2000 2005 2010 2015 {
		
		sort country year

		cap drop PollMap`year'
		gen PollMap`year' = 0
		
		if `year' == 2010 drop in 162
		if `year' == 2015 drop in 167

		*No Adoption
		list id if year==`year' & evrr1==0
		if `year' == 1990 local visit 1 2 48 49 50 55 84 85 86 93 6 7 9 63 67 69 72 21 120 44 5 106 109 110 112 95 22 23 24 25 46 128 131 133 134 59 61 29 32 10 81 74 12 15 77 79 80 
		if `year' == 1995 local visit 48 49 55 84 86 93 6 9 63 115 67 69 72 21 5 106 109 110 112 95 22 24 45 46 134 59 29 32 81 74 12 77 79 80
		if `year' == 2000 local visit 49 55 84 86 93 6 9 63 115 67 72 21 106 109 110 112 95 22 24 45 46 134 59 29 32 10 81 74 12 77 79 80
		if `year' == 2005 local visit 49 55 6 9 63 115 67 72 21 106 109 110 112 95 22 24 45 46 134 29 32 10 81 74 12 77 79 80
		if `year' == 2010 local visit 55 6 9 63 115 67 72 109 110 112 95 22 45 46 134 29 32 74 12 77 79 80 
		if `year' == 2015 local visit 115 67 109 110 95 22 45 134 32 74 12 77 79 80 
		foreach y of local visit{
			replace PollMap`year' = 1 if id==`y'
		}

		*No Implementation 
		list id if year==`year' & evrr1==1 & Polls_Map==0
		if `year' == 1990 local visit 41
		if `year' == 1995 local visit 2 85 7 128 131 133 15 
		if `year' == 2000 local visit 2 41 44 5 128 131 133 15 
		if `year' == 2005 local visit 2 41 50 86 93 44 5 131 133 15 
		if `year' == 2010 local visit 2 41 49 86 93 21 44 106 133 15  
		if `year' == 2015 local visit 2 49 86 93 6 63 72 21 133 29 15 34		
		foreach y of local visit{
			replace PollMap`year' = 2 if id==`y'
		}

		*Diplomatic Polling Stations 
		list id if year==`year' & evrr1==1 & Polls_Map==1
		if `year' == 1990 local visit 141
		if `year' == 1995 local visit 41 50 120 141 23
		if `year' == 2000 local visit 7 50 85 69 120 141 
		if `year' == 2005 local visit 7 84 85 69 120 59 141
		if `year' == 2010 local visit 7 48 50 84 85 69 131 141 10 
		if `year' == 2015 local visit 7 41 48 50 85 69 106 24 131 141 10 
		foreach y of local visit{
			replace PollMap`year' = 3 if id==`y'
		}

		*Neighborhood Polling Station 
		list id if year==`year' & evrr1==1 & Polls_Map==2
		if `year' == 1990 local visit 91 
		if `year' == 1995 local visit 91 44 61 10 
		if `year' == 2000 local visit 48 91 23 61
		if `year' == 2005 local visit 48 91 23 128 61 
		if `year' == 2010 local visit 91 120 5 23 24 128 59 61  
		if `year' == 2015 local visit 55 91 84 120 44 5 23 128 59 61  
		foreach y of local visit{
			replace PollMap`year' = 4 if id==`y'
		}

		*Arab Country* 
		list id if year==`year' & un_region==15
		if `year' == 1990 local visit 1 9 112 46 81 17 34
		if `year' == 1995 local visit 1 9 112 46 81 17 34
		if `year' == 2000 local visit 1 9 112 46 81 17 34 
		if `year' == 2005 local visit 1 9 112 46 81 17 34 
		if `year' == 2010 local visit 1 9 112 46 81 17 34
		if `year' == 2015 local visit 1 9 112 46 81 17 
		
		foreach y of local visit{
			replace PollMap`year' = 5 if id==`y'
		}
	}
	save Wellman_Maps_Working_Polls.dta, replace
	
// Graph maps
	foreach year in 1990 1995 2000 2005 2010 2015 {
		*****************
		*A2.3:`year' Map*
		*****************
		use Wellman_Maps_Working_Polls.dta, clear
		keep if year == `year'
		drop if missing(id) 
		// 1990 has a different legend than the other years
		if `year' == 1990{
			spmap PollMap`year' using africacoor.dta, id(id) fcolor(Greys) ///
				title("`year'", si(*2)) cln(12) legend(symy(*2) symx(*2) size(*1.5) ///
				label(4 "North Africa (N/A)"))legend(label(3 "No implementation")) ///
				legend(label(2 "No emigrant enfranchisement"))
		}
		else {
			spmap PollMap`year' using africacoor.dta, id(id) fcolor(Greys) ///
				title("`year'", si(*2)) cln(12) legend(symy(*2) symx(*2) size(*1.5) ///
				 label(6 "North Africa (N/A)"))legend(label(5 "Neighborhood Polls"))legend(label(4 "Diplomatic 	Polls"))legend(label(3 "No implementation")) legend(label(2 "No emigrant enfranchisement"))
		}
		
			
		graph export Polls`year'.pdf, as(pdf) replace
	}
	

